/**
 * 6vhao.com 最新电影爬虫
 * 关联二级页面爬取
 * 图片保存到 sm.ms 图床
 **/

const puppeteer = require('puppeteer');
const fs = require("fs");
const smmsx = require('smmsx');
const savePath = '6vhao_latest.dat';

const timestart = new Date().getTime();

(async() => {
	// 默认启动内嵌的 chrome
	const browser = await puppeteer.launch(
		{args: ['--no-sandbox']}
	);
	// open webpage
	const page = await browser.newPage();
	await page.setRequestInterception(true);
	page.on('request', interceptedRequest => {
		const url = interceptedRequest.url().toLowerCase();
		for (name of ['.jpg', '.jpeg', '.png', '.gif']){
			if (url.endsWith(name)){
				// 终止请求
				interceptedRequest.abort();
				return ;
			}
		}
		interceptedRequest.continue();
	});
	await page.goto('http://www.hao6v.com/', {waitUntil: 'domcontentloaded'});
	const listHdlArr = await page.$x('//div[@class="box" and h3/text()="★2017最新电影★ "]//li');
	if (listHdlArr == null){
		console.log('找不到最新电影列表');
		return ;
	}
	// 逐个处理 列表项
	let result = [];
	await (async()=>{
		for (liHdl of listHdlArr){
			// url
			const urlHdl = await liHdl.$x('a/@href');
			const url = await textValue(urlHdl[0], page);
			result.push({ url })
		}
	})();
	
	// 抓详情页
	let ptask = [];
	result.forEach(item=>{
		ptask.push((async()=>{
			const page = await browser.newPage();
			await page.setRequestInterception(true);
			page.on('request', interceptedRequest => {
				const url = interceptedRequest.url().toLowerCase();
				for (name of ['.jpg', '.jpeg', '.png', '.gif']){
					if (url.endsWith(name)){
						// 终止请求
						interceptedRequest.abort();
						return ;
					}
				}
				interceptedRequest.continue();
			});

			await page.goto(item.url, {waitUntil: 'domcontentloaded'});
			// 抽标题
			const titleHdl = await page.$x('.//*[@class="box"]/h1/text()');
			const title = await textValue(titleHdl[0], page);
			item.title = title;
			// 描述
			const descHdl = await page.$x('.//*[@id="endText"]');
			const desc = await page.evaluate(e=> e.innerHTML, descHdl[0]);
			item.desc = desc;
			// 海报
			const posterHdl = await page.$x('.//*[@id="endText"]//img/@src');
			const poster = await textValue(posterHdl[0], page);
			item.poster = poster;
		})())
	});
	await Promise.all(ptask);
	// 保存图片到图床
	for (item of result){
		const tmpfile = '/home/linglq/tmp/' + (new Date().getTime()) + Math.random() + '.jpg';
		try {
		await smmsx.resize(item.poster, tmpfile, 120, null);
			let ret = await smmsx.upload(tmpfile);
			ret = JSON.parse(ret);
			if (ret && ret.data.url){
				item.poster = ret.data.url;
			}
		} catch (e){
			console.log(e);
		}
	}
	// 保存文件
	fs.writeFile(savePath, JSON.stringify(result), {
			encoding: 'utf8',
			mode: 0666,
			flag: 'w'
		}, err=>{
		if (err){
			return console.error(err);
		}
	})
	// debug
	console.log(JSON.stringify(result, null, 4))
	
	// close browser
	await browser.close();

	const timeend = new Date().getTime();
	console.log(timestart, timeend, timeend - timestart);
})();

async function textValue(jsHandle){
	const text = await jsHandle.executionContext().evaluate(e=> e.textContent, jsHandle);
	return text;
}
